import { defineStore } from "pinia"
import http from "../utils/http"
import { message } from 'ant-design-vue';
export const useHome = defineStore("home", {
    state: () => {
        return {
            imgs: [],
            textLists: [],
            category: [],
            tag: [],
            total: 0,
            isadd:"",
        }
    },
    actions: {
        //获取文章数据
        async getImages(page, pageSize) {
            let res = await http.get("/api/article", { params: { page, pageSize } });
            if (res.status === 200) {
                //处理后端发送过来的数据
                let newarr = res.data.data[0].map((v, i) => {
                    v.createAt = new Date(v.createAt).toLocaleString()
                    v.key = i
                    return v
                })
                this.imgs = newarr,
                    //获取数据的总数
                    this.total = res.data.data[1]
            }
        },
        //获取评论数据
        async getList(page, pageSize) {
            let res = await http.get("/api/comment", { params: { page, pageSize } })
            if (res.status === 200) {
                //处理后端发送过来的数据
                let newarr = res.data.data[0].map((v, i) => {
                    v.createAt = new Date(v.createAt).toLocaleString()
                    v.key = i
                    return v
                })
                this.textLists = newarr,
                    //获取数据的总数
                    this.total = res.data.data[1]
            }
        },

        //文章模糊搜索数据
        async getSearchs(value) {
            let res = await http.get("/api/article", { params: value })
            if (res.status === 200) {
                //处理后端发送过来的数据
                let newarr = res.data.data[0].map((v, i) => {
                    v.createAt = new Date(v.createAt).toLocaleString()
                    v.key = i
                    return v
                })
                this.imgs = newarr,
                    //获取数据的总数
                    this.total = res.data.data[1]
            }
        },
        //获取分类数据
        async getcategory() {
            let res = await http.get("/api/category")
            // console.log(res.data.data,"category res")
            if (res.status === 200) {
                this.category = res.data.data
            }
        },
        //增加分类标签
        async addcategory(obj) {
            let res = await http.post("/api/category", obj)
            if (res.status === 201) {
                this.getcategory()
            }
        },
        //删除分类标签
        async removecategory(id) {
            let res = await http.delete("/api/category/" + id)
            if (res.status === 200) {
                this.getcategory()
            }
        },
        //更新分类标签
        async editcategory(id, obj) {
            let res = await http.patch("/api/category/" + id, obj);
            if (res.status === 200) {
                this.getcategory()
            }
        },
        //获取标签数据
        async gettag() {
            let res = await http.get("/api/tag")
            // console.log(res.data.data,"tag res")
            if (res.status === 200) {
                this.tag = res.data.data
            }
        },
        //增加标签标签
        async addtag(obj) {
            let res = await http.post("/api/tag", obj)
            if (res.status === 201) {
                this.gettag()
            }
        },
        //删除标签标签
        async removetag(id) {
            let res = await http.delete("/api/tag/" + id)
            if (res.status === 200) {
                this.gettag()
            }
        },
        //更新标签标签
        async edittag(id, obj) {
            let res = await http.patch("/api/tag/" + id, obj);
            if (res.status === 200) {
                this.gettag()
            }
        },
        //更改首焦状态
        async setRecom(obj) {
            let res = await http.patch("/api/article/" + obj.id, { isRecommended: obj.isRecommended })
            if (res.status == 200) {
                this.getSearchs({ page: 1, pageSize: 12 })
            }
        },
        //更改多选首焦
        async setallrecom(arr, acc) {
            [...arr].map(v => {
                let obj = {
                    id: this.imgs[v].id,
                    isRecommended: acc
                }
                this.setRecom(obj)
                // console.log(obj,"111")
            })
        },
        //更改 文章的 发布 或 草稿
        async editpublishs(obj) {
            let res = await http.patch("/api/article/" + obj.id, { status: obj.status });
            if (res.status == 200) {
                this.getSearchs({ page: 1, pageSize: 12 })
            }
        },
        //批量发布
        pageallpublish(arr) {
            arr.map(item => {
                this.editpublishs({ id: this.imgs[item].id, status: "publish" })
            })
        },
        //批量下线
        pagealldraft(arr) {
            arr.map(item => {
                this.editpublishs({ id: this.imgs[item].id, status: "draft" })
            })
        },
        //删除单个数据
        async removepage(id) {
            let res = await http.delete("/api/article/" + id);
            if (res.status === 200) {
                this.getSearchs({ page: 1, pageSize: 12 })
            }
        },
        //删除多个数据
        pageallremove(arr) {
            arr.map(item => {
                this.removepage(this.imgs[item].id)
            })
        },

        //编辑文章 
        async edittexts(id,obj){
            let res = await http.patch("/api/article/"+id,obj);
            if(res.status==200){
                message.success("修改成功")
            }
        },
        //添加文章 https://creationapi.shbwyz.com/api/article
        async addtexts(obj){
            let res = await http.post("/api/article",obj);
            if(res.status == 201){
                message.success("添加成功")
                // console.log(res,"add art res")
                this.isadd = res.data.data.id;
            }
        }
    },
    getters: {

    }
})
